package com.mgkj.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgkj.common.constant.WmsConstants;
import com.mgkj.common.result.Result;
import com.mgkj.common.utils.DateUtils;
import com.mgkj.dto.*;
import com.mgkj.entity.InvBarcodeOperation;
import com.mgkj.entity.Item;
import com.mgkj.entity.PurchaseArrival;
import com.mgkj.entity.session.*;
import com.mgkj.exception.BaseException;
import com.mgkj.mapper.*;
import com.mgkj.qywx.CorpiProperties;
import com.mgkj.service.*;
import com.mgkj.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.mgkj.entity.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.mgkj.common.constant.WmsConstants.*;


/**
 * @author yyyjcg
 * @date 2023/12/26
 * @Description
 */
@Service
public class PurchaseServiceImp  implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private PurchaseArrivalMapper purchaseArrivalMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private BmBarcodeDetailMapper bmBarcodeDetailMapper;
    @Autowired
    private DocMapper docMapper;
    //    @Autowired
//    private PrintLineService printLineService;
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private POQCRESULTMapper poqcreSULTMapper;

    @Autowired
    private POQCRESULTDMapper poqcreSULTDMapper;

    @Autowired
    private POQCRESULTSDMapper  poqcreSULTSDMapper;
    @Autowired
    private PoArrivalInspectionDMapper  poarrivalInspectionDMapper;

    @Autowired
    private WmsPoArrivalInspectionMapper  wmsPoArrivalInspectionMapper;


    @Autowired
    private PoArrivalInspectionSd1Service poArrivalInspectionSd1Service ;

    @Autowired
    private PoArrivalInspectionSd1Mapper poArrivalInspectionSd1Mapper ;

    @Resource
    private WmsPoArrivalInspectionDService wmsPoArrivalInspectionDService;

    @Resource
    private WmsPoArrivalInspectionDMeasurementValueService wmsPoArrivalInspectionDMeasurementValueService;

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private CorpiProperties corpiProperties;

    @Autowired
    private QcService qcService;

    @Value("${file.upload-dir}")
    private String uploadDir;

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;








    @Override
    public Result getPurchaseReceiveInfo(PurchaseReceiveDto purchaseReceiveDto) {
        PurchaseReceiveVo vo = purchaseMapper.getPurchaseReceiveInfo(purchaseReceiveDto);
        return null;
    }

//    @Override
//    public Result getAllPurchaseInfo(PurchaseVo purchaseVo) {
//        List<PurchaseSumDto> purchaseList = new ArrayList<>();
//        String appModule = purchaseVo.getAppModule();
//        purchaseList = getFromRedis(appModule);
//        if (purchaseList!=null){
//            return Result.ok(purchaseList);
//        }
//        //采购收货
//        if (appModule.equals("A001")){
//            purchaseList = purchaseMapper.getAllPurchaseInfo1(purchaseVo);
//            if (purchaseList == null || purchaseList.isEmpty()) {
//                // 数据库中也不存在，则返回 false
//                return Result.fail("数据库中不存在！");
//            }else {
//                setPurchaseCache(purchaseList,appModule);
//            }
//        }
//        //扫码入库
//        if (appModule.equals("A005")){
//            purchaseList = purchaseMapper.getAllPurchaseInfo2(purchaseVo);
//            if (purchaseList == null || purchaseList.isEmpty()) {
//                // 数据库中也不存在，则返回 false
//                return Result.fail("数据库中不存在！");
//            }else {
//                setPurchaseCache(purchaseList,appModule);
//            }
//        }
//        //采购仓退
//        if (appModule.equals("A008")){
//            purchaseList = purchaseMapper.getAllPurchaseInfo3(purchaseVo);
//            if (purchaseList == null || purchaseList.isEmpty()) {
//                // 数据库中也不存在，则返回 false
//                return Result.fail("数据库中不存在！");
//            }else {
//                setPurchaseCache(purchaseList,appModule);
//            }
//        }
//        for (PurchaseSumDto purchaseList1 : purchaseList) {
//            System.out.println(purchaseList1);
//        }
//        return Result.ok(purchaseList);
//    }

    @Override
    public List<PurchaseDetailDto> getPurchaseDetailInfo(PurchaseVo purchaseVo) {
        List<PurchaseDetailDto> purchaseDetailDtoList = new ArrayList<>();
        if (purchaseVo.getAppModule().equals("D001")){
            purchaseDetailDtoList = purchaseMapper.getPurchaseDetailInfo(purchaseVo);
        }
        return purchaseDetailDtoList;
    }


    //查询采购单简易信息(采购单号，品号，品名，规格，采购数量)
    @Override
    @DS("db2")
    public Result getPurcharseInfo(PurchaseOrderSimpleDto purchaseOrderSimpleDto) {
        List<PurchaseOrderSimpleVo> list = purchaseMapper.getPurcharseInfo(purchaseOrderSimpleDto);
        if(list != null && !list.isEmpty()){
            return Result.ok(list);
        }else {
            return Result.fail().message("暂无数据");
        }

    }

    /**
     * 快速收货查询(可用)
     * @param dto
     * @return
     */
    @Override
    public Result<List<PurchaseOrderDetailVo>> getPurcharseDetail(PurchaseOrderDetailDto dto) {
        List<PurchaseOrderDetailVo> list = new ArrayList<>();
        try {
            list = purchaseMapper.getPurcharseDetail(dto);
            if(list != null && !list.isEmpty()){
                return Result.ok(list).message("查询成功");
            }else {
                return Result.ok(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(list).message("查询失败");
        }

    }

    /**
     * 快速收货提交（批量提交不同单号同一供应商）
     * @param fastDeliveryDto
     * @return
     */
    @Override
    public Result FastDeliveryGenerateDoc(FastDeliveryDto fastDeliveryDto) {
//        DateTime date = DateUtil.date();
//        //所有要提交的数据
//        List<ParameterDataList> allSubmitList = fastDeliveryDto.getData();
//        Collections.sort(allSubmitList,new Comparator<ParameterDataList>(){
//            @Override
//            public int compare(ParameterDataList p1, ParameterDataList p2) {
//                return p1.getDoc_no().compareTo(p2.getDoc_no());
//            }
//        });
//        //每次提交时的单号
//        String docNo="";
//        List<DocResult> docResults = new ArrayList<>();
//        //存储分次提交的数据
//        List<ParameterDataList> submitList = new ArrayList<>();
//        List<String> resultList = new ArrayList<>();
//        System.out.println("开始上传");
//        for (ParameterDataList data : allSubmitList) {
//            if (!docNo.equals(data.getDoc_no())){
//                System.out.println("docNo:"+docNo);
//                System.out.println("doc_no:"+data.getDoc_no());
//                if (!docNo.equals("")){
//                    System.out.println("第一次上传");
//                    EtenSession etenSession = new EtenSession(FASTDELIVERY_KEY,WMS_TYPE,
//                            new SessionHost(WMS_HOST_PROD,"",WMS_HOST_LANG,WMS_HOST_ACCT, FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"), "zhiLink"),
//                            new SessionService(WMS_SERVICE_PROD,FASTDELIVERY_NAME,"",WMS_SERVICE_ID),
//                            new SessionDatakey(WMS_ENTID, WMS_COMPANYID),
//                            new SessionPayload(new StdData(
//                                    new FastDeliveryParameter(WMS_ENTID,WMS_COMPANYID,docNo,submitList, DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"),"zhiLink",
//                                            new FastDeliveryParameterHost(WMS_ACCT,FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),FASTDELIVERY_APPMODULE,FASTDELIVERY_NAME, WMS_ENTID, WMS_COMPANYID, DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"), "zhiLink", "0")))));
//
//                    try {
//                        HttpHeaders httpHeaders = new HttpHeaders();
//                        httpHeaders.add("Content-Type","text/plain; charset=utf-8");
//                        System.out.println(etenSession);
//                        HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
//                        RestTemplate restTemplate = new RestTemplate();
//                        String response = restTemplate.postForObject(SERVICE_IP_PORT+"/Restful/invokeSrv", httpEntity, String.class);
//                        JSONObject result = JSON.parseObject(response);
//                        String resultDocNo = result.getJSONObject("payload")
//                                .getJSONObject("std_data")
//                                .getJSONObject("parameter")
//                                .getJSONArray("result")
//                                .getJSONObject(0)
//                                .getString("doc_no");
////                        return Result.ok(result).message("发送成功");
////                        String resultDocNo = (String) result.get("doc_no");
////                        resultList.add(response.getPayload().getStd_data().getParameter().getResult().getDoc_no());
//                        resultList.add(resultDocNo);
//                        System.out.println(docNo+"上传成功,生成单号为:"+resultDocNo);
//                        System.out.println("submitList:"+submitList);
//                        docResults.add(new DocResult(docNo,resultDocNo,submitList));
//                        System.out.println(docResults);
//                    }catch (Exception e){
//                        return Result.fail().message("上传失败请联系管理员删除上传数据1");
//                    }
////                    submitList.clear();
//                }
//                docNo = data.getDoc_no();
//                submitList.add(data);
//            }else {
//                System.out.println("其他情况");
//                submitList.add(data);
//            }
//            if (allSubmitList.indexOf(data)==allSubmitList.size()-1){
//                System.out.println("最后一个元素");
//                EtenSession etenSession = new EtenSession(FASTDELIVERY_KEY,WMS_TYPE,
//                        new SessionHost(WMS_HOST_PROD,"",WMS_HOST_LANG,WMS_HOST_ACCT, FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"), "zhiLink"),
//                        new SessionService(WMS_SERVICE_PROD,FASTDELIVERY_NAME,"",WMS_SERVICE_ID),
//                        new SessionDatakey(WMS_ENTID, WMS_COMPANYID),
//                        new SessionPayload(new StdData(
//                                new FastDeliveryParameter(WMS_ENTID,WMS_COMPANYID,docNo,submitList, DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"),"zhiLink",
//                                        new FastDeliveryParameterHost(WMS_ACCT,FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),FASTDELIVERY_APPMODULE,FASTDELIVERY_NAME, WMS_ENTID, WMS_COMPANYID, DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"), "zhiLink", "0")))));
//
//                try {
//                    HttpHeaders httpHeaders = new HttpHeaders();
//                    httpHeaders.add("Content-Type","text/plain; charset=utf-8");
//                    System.out.println(etenSession);
//                    HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
//                    RestTemplate restTemplate = new RestTemplate();
//                    String response = restTemplate.postForObject(SERVICE_IP_PORT+"/Restful/invokeSrv", httpEntity, String.class);
//                    JSONObject result = JSON.parseObject(response);
//                    System.out.println(result);
//                    String resultDocNo = result.getJSONObject("payload")
//                            .getJSONObject("std_data")
//                            .getJSONObject("parameter")
//                            .getJSONArray("result")
//                            .getJSONObject(0)
//                            .getString("doc_no");
////                        return Result.ok(result).message("发送成功");
////                        String resultDocNo = (String) result.get("doc_no");
////                        resultList.add(response.getPayload().getStd_data().getParameter().getResult().getDoc_no());
//                    resultList.add(resultDocNo);
//                    System.out.println(docNo+"上传成功,生成单号为:"+resultDocNo);
//                    docResults.add(new DocResult(docNo,resultDocNo,submitList));
//                    System.out.println(docResults);
//                }catch (Exception e){
//                    e.printStackTrace();
//                    return Result.fail(resultList).message("上传失败请联系管理员删除上传数据2");
//                }
//            }
//        }
//        return Result.ok(docResults).message("全部上传成功");
        DateTime date = DateUtil.date();
        EtenSession etenSession = new EtenSession(FASTDELIVERY_KEY,WMS_TYPE,
                new SessionHost(WMS_HOST_PROD,"",WMS_HOST_LANG,WMS_HOST_ACCT, FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"), "zhiLink"),
                new SessionService(WMS_SERVICE_PROD,FASTDELIVERY_NAME,"",WMS_SERVICE_ID),
                new SessionDatakey(WMS_ENTID, WMS_COMPANYID),
                new SessionPayload(new StdData(
                        new FastDeliveryParameter(WMS_ENTID,WMS_COMPANYID,"",fastDeliveryDto.getData(), DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"),"zhiLink",
                                new FastDeliveryParameterHost(WMS_ACCT,FASTDELIVERY_APPID+FASTDELIVERY_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),FASTDELIVERY_APPMODULE,FASTDELIVERY_NAME, WMS_ENTID, WMS_COMPANYID, DateUtil.format(date,"yyyy-MM-dd"),DateUtil.format(date,"yyyy-MM-dd"), "zhiLink", "0")))));

        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Content-Type","text/plain; charset=utf-8");
            System.out.println(etenSession);
            HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
            RestTemplate restTemplate = new RestTemplate();
            String response = restTemplate.postForObject(SERVICE_IP_PORT+"/Restful/invokeSrv", httpEntity, String.class);
            JSONObject result = JSON.parseObject(response);
            String resultDocNo = result.getJSONObject("payload")
                    .getJSONObject("std_data")
                    .getJSONObject("parameter")
                    .getJSONArray("result")
                    .getJSONObject(0)
                    .getString("doc_no");
//                        return Result.ok(result).message("发送成功");
//                        String resultDocNo = (String) result.get("doc_no");
//                        resultList.add(response.getPayload().getStd_data().getParameter().getResult().getDoc_no());

            System.out.println("上传成功,生成单号为:"+resultDocNo);
            return Result.ok(resultDocNo).message("上传成功,生成单号为:"+resultDocNo);
        }catch (Exception e){
            return Result.fail().message("上传失败请联系管理员");
        }
    }




//    @Override
//    public Result printPurchaseArrival(String docNo) {
//        List<PurchaseArrivalPrintVo> list = purchaseMapper.printPurchaseArrival(docNo);
//        for (PurchaseArrivalPrintVo vo : list) {
//            vo.setBarcode(printLineMapper.getBarcodeByDocNoItemCode(vo.getDocNo(),vo.getItemCode(),vo.getDocDate()));
//        }
//        if (list!=null && !list.isEmpty()){
//            return Result.ok(list).message("信息获取成功");
//        }else{
//            return Result.fail().message("信息获取失败，请联系管理员");
//        }
//
//    }



    @Override
    @Transactional
    public Result producePurchaseInspectionOrder(PurchaseInspectionOrderDto dto) {
        try {
            dto.setTime(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss.SSSSSS"));
            String uuid = UUID.randomUUID().toString();
            String uuid2 = UUID.randomUUID().toString();
            String uuid3 = UUID.randomUUID().toString();
            System.out.println(uuid);
            System.out.println(dto.getTime());
            //判断是否已经存在检验单
            //不存在
            if(dto.getLastCheckStatus().equals("2")){
                //已经检查完毕
                if (dto.getPendingCheckQty()-dto.getThisCheckQty()==0){
                    dto.setResultStatus("3");
                    dto.setArrivalResultStatus("4");
                    purchaseMapper.producePurchaseInspectionOrder1(uuid,dto);
                    purchaseMapper.producePurchaseInspectionOrder2(uuid,uuid2,dto);
                    purchaseMapper.producePurchaseInspectionOrder3(uuid2,uuid3,dto);
                    purchaseMapper.updatePurchaseArrival(dto);
                }else{
                    //部分检查
                    dto.setResultStatus("2");
                    dto.setArrivalResultStatus("3");
                    purchaseMapper.producePurchaseInspectionOrder1(uuid,dto);
                    purchaseMapper.producePurchaseInspectionOrder2(uuid,uuid2,dto);
                    purchaseMapper.producePurchaseInspectionOrder3(uuid2,uuid3,dto);
                    purchaseMapper.updatePurchaseArrival(dto);
                }
            }else {
                //已经存在
                //检查检查完毕
                if (dto.getPendingCheckQty()-dto.getThisCheckQty()==0){
                    dto.setResultStatus("3");
                    dto.setArrivalResultStatus("4");
                    purchaseMapper.updatePurchaseInspectionOrder2(dto);
                    purchaseMapper.updatePurchaseInspectionOrder3(dto);
                    purchaseMapper.updatePurchaseArrival(dto);
                }else {
                    //部分检查
                    dto.setResultStatus("2");
                    dto.setArrivalResultStatus("3");
                    purchaseMapper.updatePurchaseInspectionOrder2(dto);
                    purchaseMapper.updatePurchaseInspectionOrder3(dto);
                    purchaseMapper.updatePurchaseArrival(dto);
                }
            }


            return Result.ok().message("插入成功");

        }catch (Exception e){
            e.printStackTrace();
            return Result.fail().message("插入失败");
        }

    }

    @Override
    public Result<List<PurchaseArrivalDetailVo>> ListCheckPurchase(ListCheckPurchaseDto dto) {
        List<PurchaseArrivalDetailVo> list = new ArrayList<>();
        try {
            list = purchaseMapper.ListCheckPurchase(dto);
            if (list!=null && !list.isEmpty()){
                return Result.ok(list).message("查询成功");
            }else {
                return Result.ok(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(list).message("查询失败");
        }


    }

    @Override
    public Result ListPurchaseReturn(ListPurchaseReturnDto dto) {
        try {
            List<ListPurchaseReturnVo> list = purchaseMapper.ListPurchaseReturn(dto);
            if (list!=null && !list.isEmpty()){
                return Result.ok(list).message("查询成功");
            }else {
                return Result.ok(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail().message("查询失败联系管理员");
        }
    }

    @Override
    public Result ListPurchaseReturnDetail(String docNo) {
        try {
            List<ListPurchaseReturnDetailVo> list = purchaseMapper.ListPurchaseReturnDetail(docNo);
            if (list!=null && !list.isEmpty()){
                return Result.ok(list).message("查询成功");
            }else {
                return Result.ok(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail().message("查询失败联系管理员");
        }
    }


    @Override
    public MiddleReturnDto PurchaseReturnInsertMiddleTable(List<PurchaseReturnSubmitDto> list) {
        DateTime date = DateUtil.date();
        List<String> docNo = new ArrayList<>();
        for (PurchaseReturnSubmitDto dto : list) {
//            String lotNo = printLineService.getLotNoFromBarcode(dto.getBarcode(),dto.getItemCode());
            LambdaQueryWrapper<Item> queryWrapper  = new LambdaQueryWrapper<Item>().eq(Item::getItemCode, dto.getItemCode()).eq(Item::getLotControl, "T");
            //开启批号的
            if (itemMapper.selectCount(queryWrapper) > 0) {
                String barcode = dto.getBarcode();
                docNo.add(dto.getDocNo());
                purchaseMapper.insertMiddleTable2(Lsmdt.builder()
                                .lsmdTId(UUID.randomUUID().toString().toUpperCase()) //主键
                                .lsmd001(barcode)      //条码编号
                                .lsmd002(dto.getWarehouseCode())      //仓库
                                .lsmd003(dto.getBinCode())//库位
                                .lsmd004(barcode.substring(barcode.indexOf("#") + 1))//批号
//                            .lsmd004("")//批号
                                .lsmd005(dto.getMatchQty())//异动数量
                                .lsmd006(-1)//异动类型
                                .lsmd007(dto.getDocNo())//来源单号
                                .lsmd008("0")
                                .lsmd010("0")
                                .lsmd013("admin")//PDA操作人代号
                                .lsmd014(DateUtil.format(date, "yyyy-MM-dd"))//扫描日期
                                .lsmd015(DateUtil.format(date, "HH:mm:ss.SSS"))//时间
                                .lsmd019("Y")//扣账资料否
                                .lsmd024(dto.getUnitCode())//异动数量单位
                                .lsmd025(1.0)//异动与库存单位换算率
                                .lsmd026(PURCHASERETURN_APPID)//终端设备编号
                                .lsmd027(PURCHASERETURN_APPID + PURCHASERETURN_APPMODULE + WMS_APPVERSION + DateUtil.format(date, "yyMMddhhmmss"))//APP申请串号
                                .lsmd028(PURCHASERETURN_APPMODULE)//APP申请所属模组
                                .lsmd029("N")//ERP异动码
                                .lsmd030("")//备注
                                .lsmd031("1")//条码异动类型
                                .lsmd033(UUID.randomUUID().toString().trim().replace("-", ""))//APP申请序列号
                                .lsmd036(dto.getBinCode())
                                .lsmd037(barcode.substring(barcode.indexOf("#") + 1))
//                            .lsmd037("")
                                .lsmd038("")
                                .lsmd039("002")
                                .lsmd042("NNN")
                                .lsmd045("0.0")
                                .lsmd046(dto.getUnitCode())
                                .lsmd047("0.0")

                                .lsmd053(0.00)
                                .lsmd054("0.00")
                                .lsmd055(0.00)
                                .lsmd058("9998-12-31")
                                .lsmd081("0.0")
                                .lsmd082("0.0")
                                .lsmd083(dto.getItemCode())
                                .lsmd084(UUID.randomUUID().toString().trim().replace("-", ""))//字符保留栏位
                                .lsmd085(DateUtil.format(DateUtil.date(), "yyyy-MM-dd"))//过账日期
                                .lsmd086(DateUtil.format(DateUtil.date(), "yyyy-MM-dd"))//日期保留栏位
                                .lsmd906("0")
                                .lsmd907("0")
                                .lsmdcrtdt(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))//资料创建日
                                .lsmdcrtid("1")//资料建立者
                                .lsmdent(WMS_ENTID)//企业代码
                                .lsmdmoddt(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))//最近修改日
                                .lsmdmodid("1") //资料修改者
                                .lsmdowndp("qqq") //资料所属部门
                                .lsmdownid("dcms") //资料所有者
                                .lsmdsite(WMS_COMPANYID) //营运据点
                                .lsmdstus("Y") //状态码
                                .build()
                );
            }else {
                String barcode = dto.getBarcode();
                docNo.add(dto.getDocNo());
                purchaseMapper.insertMiddleTable2(Lsmdt.builder()
                                .lsmdTId(UUID.randomUUID().toString().toUpperCase()) //主键
                                .lsmd001(barcode)      //条码编号
                                .lsmd002(dto.getWarehouseCode())      //仓库
                                .lsmd003(dto.getBinCode())//库位
                                .lsmd004(barcode.substring(barcode.indexOf("#") + 1))//批号
//                            .lsmd004("")//批号
                                .lsmd005(dto.getMatchQty())//异动数量
                                .lsmd006(-1)//异动类型
                                .lsmd007(dto.getDocNo())//来源单号
                                .lsmd008("0")
                                .lsmd010("0")
                                .lsmd013("admin")//PDA操作人代号
                                .lsmd014(DateUtil.format(date, "yyyy-MM-dd"))//扫描日期
                                .lsmd015(DateUtil.format(date, "HH:mm:ss.SSS"))//时间
                                .lsmd019("Y")//扣账资料否
                                .lsmd024(dto.getUnitCode())//异动数量单位
                                .lsmd025(1.0)//异动与库存单位换算率
                                .lsmd026(PURCHASERETURN_APPID)//终端设备编号
                                .lsmd027(PURCHASERETURN_APPID + PURCHASERETURN_APPMODULE + WMS_APPVERSION + DateUtil.format(date, "yyMMddhhmmss"))//APP申请串号
                                .lsmd028(PURCHASERETURN_APPMODULE)//APP申请所属模组
                                .lsmd029("N")//ERP异动码
                                .lsmd030("")//备注
                                .lsmd031("1")//条码异动类型
                                .lsmd033(UUID.randomUUID().toString().trim().replace("-", ""))//APP申请序列号
                                .lsmd036("")
                                .lsmd037("")
                                .lsmd038("")
                                .lsmd039("002")
                                .lsmd042("NNN")
                                .lsmd045("0.0")
                                .lsmd046(dto.getUnitCode())
                                .lsmd047("0.0")
                                .lsmd053(0.00)
                                .lsmd054("0.00")
                                .lsmd055(0.00)
                                .lsmd058("9998-12-31")
                                .lsmd081("0.0")
                                .lsmd082("0.0")
                                .lsmd083(dto.getItemCode())
                                .lsmd084(UUID.randomUUID().toString().trim().replace("-", ""))//字符保留栏位
                                .lsmd085(DateUtil.format(DateUtil.date(), "yyyy-MM-dd"))//过账日期
                                .lsmd086(DateUtil.format(DateUtil.date(), "yyyy-MM-dd"))//日期保留栏位
                                .lsmd906("0")
                                .lsmd907("0")
                                .lsmdcrtdt(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))//资料创建日
                                .lsmdcrtid("1")//资料建立者
                                .lsmdent(WMS_ENTID)//企业代码
                                .lsmdmoddt(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))//最近修改日
                                .lsmdmodid("1") //资料修改者
                                .lsmdowndp("qqq") //资料所属部门
                                .lsmdownid("dcms") //资料所有者
                                .lsmdsite(WMS_COMPANYID) //营运据点
                                .lsmdstus("Y") //状态码
                                .build()
                );
            }
        }
        return new MiddleReturnDto(docNo.get(0),PURCHASERETURN_APPID,PURCHASERETURN_APPID+PURCHASERETURN_APPMODULE+WMS_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),"");
    }

    /**
     * 扫码入库-同步中间表
     * @param list
     * @return
     */
    @Override
    public MiddleReturnDto PurchaseScanToStorageInsertMiddleTable(List<PurchaseToStorageDto> list) {
        System.out.println("同步中间表");
        DateTime date = DateUtil.date();
        List<String> docNo = new ArrayList<>();
        String shjg = "";
        try {
            for (PurchaseToStorageDto dto : list) {
                // 查询是否启用了批号管理
                boolean isLotControlEnabled = itemMapper.selectCount(
                        new LambdaQueryWrapper<Item>()
                                .eq(Item::getItemCode, dto.getItemCode())
                                .ne(Item::getLotControl, "N")
                ) > 0;

                // 查询是否启用了库位管理
//                boolean isBinControlEnabled = itemMapper.selectBinControl(dto.getItemCode()) == 1;

//                System.out.println("商品：" + dto.getItemCode() + " 批号管理：" + isLotControlEnabled + " 库位管理：" + isBinControlEnabled);
                System.out.println("商品：" + dto.getItemCode() + " 批号管理：" + isLotControlEnabled );

                String barcode = dto.getBarcode();
                String binCode = dto.getBinCode();
                String lotNumber = purchaseMapper.selectLotNumber(dto.getBarcode());
                shjg = dto.getShjg();
                docNo.add(dto.getDocNo());

                purchaseMapper.insertMiddleTable3(
                        Lsmdt.builder()
                                .lsmdTId(UUID.randomUUID().toString().toUpperCase()) //主键
                                .lsmd001(barcode)      //条码编号
                                .lsmd002(dto.getWarehouseCode())      //仓库
                                .lsmd003(dto.getBinCode())//库位
    //                            .lsmd004(barcode.substring(barcode.indexOf("#")+1))//批号
                                .lsmd004(lotNumber) // 批号
                                .lsmd005(dto.getMatchQty())//异动数量
                                .lsmd006(1)//异动类型
                                .lsmd007(dto.getDocNo())//来源单号
                                .lsmd008("0")
                                .lsmd010("0")
                                .lsmd013("admin")//PDA操作人代号
                                .lsmd014(DateUtil.format(date,"yyyy-MM-dd"))//扫描日期
                                .lsmd015(DateUtil.format(date,"HH:mm:ss.SSS"))//时间
                                .lsmd019("Y")//扣账资料否
                                .lsmd024(dto.getUnitCode())//异动数量单位
                                .lsmd025(1.0)//异动与库存单位换算率
                                .lsmd026(PURCHASE_SCAN_INSTORAGE_APPID)//终端设备编号
                                .lsmd027(PURCHASE_SCAN_INSTORAGE_APPID+PURCHASE_SCAN_INSTORAGE_APPMODULE+PURCHASE_INSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddHHmmss"))//APP申请串号
                                .lsmd028(PURCHASE_SCAN_INSTORAGE_APPMODULE)//APP申请所属模组
                                .lsmd029("N")//ERP异动码
                                .lsmd030(dto.getRemark())//备注
                                .lsmd031("1")//条码异动类型
                                .lsmd033(UUID.randomUUID().toString().trim().replace("-",""))//APP申请序列号
                                .lsmd036(binCode) // 如果开启库位管理，赋值库位，否则为空
                                .lsmd037(isLotControlEnabled ? lotNumber : "") // 如果开启批号管理，赋值批号，否则为空
                                .lsmd038("")
                                .lsmd039("004")
                                .lsmd042("NNY")
                                .lsmd045("0.0")
                                .lsmd046(dto.getUnitCode())
                                .lsmd047("0.0")
                                .lsmd048("批次说明")
                                .lsmd053(dto.getScrapQty())
                                .lsmd054("0.00")
                                .lsmd055(dto.getRejectQty())
                                .lsmd058("9998-12-31")
                                .lsmd081("0.0")
                                .lsmd082("0.0")
                                .lsmd083(dto.getItemCode())
                                .lsmd084(UUID.randomUUID().toString().trim().replace("-",""))//字符保留栏位
                                .lsmd085(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//过账日期
                                .lsmd086(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//日期保留栏位
                                .lsmd906("0")
                                .lsmd907("0")
                                .lsmdcrtdt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//资料创建日
                                .lsmdcrtid("1")//资料建立者
                                .lsmdent(WMS_ENTID)//企业代码
                                .lsmdmoddt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//最近修改日
                                .lsmdmodid("1") //资料修改者
                                .lsmdowndp("qqq") //资料所属部门
                                .lsmdownid("dcms") //资料所有者
                                .lsmdsite(shjg) //营运据点
                                .lsmdstus("Y") //状态码
                                .build()
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MiddleReturnDto(docNo.get(0),PURCHASE_SCAN_INSTORAGE_APPID,PURCHASE_SCAN_INSTORAGE_APPID+PURCHASE_SCAN_INSTORAGE_APPMODULE+PURCHASE_INSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddHHmmss"),"zhilink",shjg);

//        return new MiddleReturnDto(docNo.get(0),SCANTOSTORAGE_APPID,"unknown02:00:00:00:00:00ab1cbf8632fb41ceA0054.223240307153009","");

    }

    @Override
    public JSONObject PurchaseReturnStockSubmit(MiddleReturnDto middleReturnDto) {
        EtenSession etenSession = new EtenSession(WMS_SUBMIT_KEY,WMS_TYPE,
                new SessionHost(WMS_HOST_PROD,WMS_HOST_IP,WMS_HOST_LANG,WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getErpRemark()),
                new SessionService(WMS_SERVICE_PROD,PURCHASERETURN_NAME,WMS_SERVICE_IP,WMS_SERVICE_ID),
                new SessionDatakey(WMS_ENTID,WMS_COMPANYID),
                new SessionPayload(new StdData(new ScanToStorageParameter(
                        "","","","","","","","","","","","","","",
                        new ScanToStorageParameterData(
                                "", middleReturnDto.getDocNo(), middleReturnDto.getDocNo(), "","","","","","","","","","","","",""
                        ),
                        new ScanToStorageParameterHost(
                                WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getAppid(),PURCHASERETURN_APPMODULE,WMS_APPVERSION,PURCHASERETURN_NAME,WMS_ENTID,WMS_COMPANYID,middleReturnDto.getErpRemark(),"0",""
                        )
                ))));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type","text/plain; charset=utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(SERVICE_IP_PORT+"/Restful/invokeSrv", httpEntity, String.class);
        System.out.println(response);
        System.out.println(httpEntity.getBody());
        JSONObject result = JSON.parseObject(response);
        return result;
    }

    @Override
    public MiddleReturnDto PurchaseToStorageInsertMiddleTable(List<PurchaseToStorageDto> list) {
        System.out.println("同步中间表");
        DateTime date = DateUtil.date();
        List<String> docNo = new ArrayList<>();
        for (PurchaseToStorageDto dto : list) {
            String barcode = dto.getBarcode();
            docNo.add(dto.getDocNo());
            purchaseMapper.insertMiddleTable2(Lsmdt.builder()
                            .lsmdTId(UUID.randomUUID().toString().toUpperCase()) //主键
                            .lsmd001(barcode)      //条码编号
                            .lsmd002(dto.getWarehouseCode())      //仓库
                            .lsmd003(dto.getBinCode())//库位
//                    .lsmd004(barcode.substring(barcode.indexOf("#")+1))//批号
//                    达威联调时wms与e10的批号管理不一致，先全改为* /""
                            .lsmd004("")//批号
                            .lsmd005(dto.getMatchQty())//异动数量
                            .lsmd006(1)//异动类型
                            .lsmd007(dto.getDocNo())//来源单号
                            .lsmd008("0")
                            .lsmd010("0")
                            .lsmd013("admin")//PDA操作人代号
                            .lsmd014(DateUtil.format(date,"yyyy-MM-dd"))//扫描日期
                            .lsmd015(DateUtil.format(date,"HH:mm:ss.SSS"))//时间
                            .lsmd019("Y")//扣账资料否
                            .lsmd024(dto.getUnitCode())//异动数量单位
                            .lsmd025(1.0)//异动与库存单位换算率
                            .lsmd026(PURCHASE_INSTORAGE_APPID)//终端设备编号
                            .lsmd027(PURCHASE_INSTORAGE_APPID+PURCHASE_INSTORAGE_APPMODULE+PURCHASE_INSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"))//APP申请串号
                            .lsmd028(PURCHASE_INSTORAGE_APPMODULE)//APP申请所属模组
                            .lsmd029("N")//ERP异动码
                            .lsmd030("")//备注
                            .lsmd031("1")//条码异动类型
                            .lsmd033(UUID.randomUUID().toString().trim().replace("-",""))//APP申请序列号
//                    .lsmd037(barcode.substring(barcode.indexOf("#")+1))
//                    达威联调时wms与e10的批号管理不一致，先全改为* /""
                            .lsmd037("")
                            .lsmd039("002")
                            .lsmd042("NNN")
                            .lsmd045("0.0")
                            .lsmd046(dto.getUnitCode())
                            .lsmd047("0.0")
                            .lsmd053(0.00)
                            .lsmd054("0.00")
                            .lsmd055(0.00)
                            .lsmd058("9998-12-31")
                            .lsmd081("0.0")
                            .lsmd082("0.0")
                            .lsmd083(dto.getItemCode())
                            .lsmd084(UUID.randomUUID().toString().trim().replace("-",""))//字符保留栏位
                            .lsmd085(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//过账日期
                            .lsmd086(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//日期保留栏位
                            .lsmd906("0")
                            .lsmd907("0")
                            .lsmdcrtdt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//资料创建日
                            .lsmdcrtid("1")//资料建立者
                            .lsmdent(WMS_ENTID)//企业代码
                            .lsmdmoddt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//最近修改日
                            .lsmdmodid("1") //资料修改者
                            .lsmdowndp("qqq") //资料所属部门
                            .lsmdownid("dcms") //资料所有者
                            .lsmdsite(WMS_COMPANYID) //营运据点
                            .lsmdstus("Y") //状态码
                            .lsmd036(dto.getBinCode())
                            .build()
            );
        }
        return new MiddleReturnDto(docNo.get(0),PURCHASE_INSTORAGE_APPID,PURCHASE_INSTORAGE_APPID+PURCHASE_INSTORAGE_APPMODULE+PURCHASE_INSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),"采购入库测试");

//        return new MiddleReturnDto(docNo.get(0),SCANTOSTORAGE_APPID,"unknown02:00:00:00:00:00ab1cbf8632fb41ceA0054.223240307153009","");

    }

    @Override
    public Result<List<PurchaseDetailVo>> getPurchaseDetail(String docNo) {
        try {
            List<PurchaseDetailVo> list = purchaseMapper.getPurchaseDetail(docNo);
            if (!list.isEmpty()){
                return Result.ok(list);
            }else {
                return Result.fail(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }    }

    @Override
    public MiddleReturnDto PurchaseReceiptStorageInsertMiddleTable(List<PurchaseReceiptStorageDto> list) {
        System.out.println("同步中间表");
        DateTime date = DateUtil.date();
        List<String> docNo = new ArrayList<>();

            String shjg = "";
            String timestamp = PURCHASE_RECEIPT_APPID+PURCHASE_RECEIPT_APPMODULE+PURCHASE_INSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddHHmmss");
            purchaseMapper.deleteBytimestamp(timestamp);
            for (PurchaseReceiptStorageDto dto : list) {
                //设置工厂编号(收货机构)
                WmsConstants.WMS_COMPANYID = dto.getShjg();

                // 查询是否启用了批号管理
                boolean isLotControlEnabled = itemMapper.selectCount(
                        new LambdaQueryWrapper<Item>()
                                .eq(Item::getItemCode, dto.getItemCode())
                                .ne(Item::getLotControl, "N")
                ) > 0;

                // 查询是否启用了库位管理
//                boolean isBinControlEnabled = itemMapper.selectBinControl(dto.getItemCode()) == 1;

//                System.out.println("商品：" + dto.getItemCode() + " 批号管理：" + isLotControlEnabled + " 库位管理：" + isBinControlEnabled);

                // 通过条码扫到是哪个采购单的哪一行
                String orderNum = purchaseMapper.getPurchaseDByBarcode(dto.getBarcode());
                if(orderNum == null || orderNum.trim().equals("")) orderNum = "0";

                // 解析条码
                String barcode = dto.getBarcode();
                docNo.add(dto.getDocNo());
                String binCode = dto.getBinCode();
                shjg = dto.getShjg();
                String lotNumber = purchaseMapper.selectLotNumber(dto.getBarcode());
//                String lotNumber = "250523";
                purchaseMapper.insertMiddleTable3(
                        Lsmdt.builder()
                                .lsmdTId(UUID.randomUUID().toString().toUpperCase()) //主键
                                .lsmd001(barcode)      //条码编号
                                .lsmd002(dto.getWarehouseCode())      //仓库
                                .lsmd003(dto.getBinCode())//库位
                                .lsmd004(lotNumber) // 批号
                                .lsmd005(dto.getMatchQty())//异动数量
                                .lsmd006(0)//异动类型
                                .lsmd007(dto.getDocNo())//来源单号
                                .lsmd008(orderNum)  // 来源项次
                                .lsmd010("0")
                                .lsmd013("admin")//PDA操作人代号
                                .lsmd014(DateUtil.format(date,"yyyy-MM-dd"))//扫描日期
                                .lsmd015(DateUtil.format(date,"HH:mm:ss.SSS"))//时间
                                .lsmd019("Y")//扣账资料否
                                .lsmd024(dto.getUnitCode())//异动数量单位
                                .lsmd025(1.0)//异动与库存单位换算率
                                .lsmd026(PURCHASE_RECEIPT_APPID)//终端设备编号
                                .lsmd027(timestamp)//APP申请串号
                                .lsmd028(PURCHASE_RECEIPT_APPMODULE)//APP申请所属模组
                                .lsmd029("N")//ERP异动码
                                .lsmd030(dto.getRemark())//备注
//                                .lsmd031("")//条码异动类型
                                .lsmd033(UUID.randomUUID().toString().trim().replace("-",""))//APP申请序列号
                                .lsmd036(binCode) // 如果开启库位管理，赋值库位，否则为空
    //                            .lsmd036(isBinControlEnabled ? binCode : "") // 如果开启库位管理，赋值库位，否则为空
                                .lsmd037(isLotControlEnabled ? lotNumber : "") // 如果开启批号管理，赋值批号，否则为空
                                .lsmd038("")
                                .lsmd039("002")
                                .lsmd042("NNN")
                                .lsmd045("0.0")
                                .lsmd046(dto.getUnitCode())
                                .lsmd047("0.0")
                                .lsmd048("")
                                .lsmd053(dto.getScrapQty())
                                .lsmd054("0.00")
                                .lsmd055(dto.getRejectQty())
                                .lsmd058("9998-12-31")
                                .lsmd081("0.0")
                                .lsmd082("0.0")
                                .lsmd083(dto.getItemCode())
                                .lsmd084(UUID.randomUUID().toString().trim().replace("-",""))//字符保留栏位
                                .lsmd085(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//过账日期
                                .lsmd086(DateUtil.format(DateUtil.date(),"yyyy-MM-dd"))//日期保留栏位
                                .lsmd906("0")
                                .lsmd907("0")
                                .lsmdcrtdt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//资料创建日
                                .lsmdcrtid("1")//资料建立者
                                .lsmdent(WMS_ENTID)//企业代码
                                .lsmdmoddt(DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"))//最近修改日
                                .lsmdmodid("1") //资料修改者
                                .lsmdowndp("qqq") //资料所属部门
                                .lsmdownid("dcms") //资料所有者
                                .lsmdsite(shjg) //营运据点
                                .lsmdstus("Y") //状态码
                                .build()
                );
            }

        return new MiddleReturnDto(docNo.get(0),PURCHASE_RECEIPT_APPID,timestamp,"zhilink",shjg);

    }


    @Override
    public List<PurchaseReceiptStorageVo> getPurcharsedeliveryNo(PurcharseDeliveryDto dto) {
        // 查询条码信息
        List<PurchaseReceiptStorageVo> list =purchaseMapper.getBarCodeByDeliveryNo(dto);
        return list;
    }

    //子码采购限制
    private void barcodeLimitByPurchase(String barcode) {
        BarcodeDetails b = purchaseMapper.getBarcodeDetails(barcode);

        if(b.getLot_att30() != null){
           throw new RuntimeException("该条码已生成过到货单，请勿重复生成！");
        }

        // 校验关键字段是否为空
        if (b == null || b.getPrice() == null || b.getWarehouseCode() == null || b.getPlanArrivalDate() == null) {
            return;
        }

        // 校验价格是否为0
        if (BigDecimal.ZERO.compareTo(b.getPrice()) == 0) {
            String errorMsg = String.format(
                    "%s,%s,%s,%s,采购单价为0,不允许到货,请联系采购员:%s",
                    b.getPurchaseOrder(), b.getCustomerNo(), b.getItemCode(), b.getItemName(), b.getEmployeeName()
            );
            throw new RuntimeException(errorMsg);
        }

        // 计算允许的最早预交货日期
        LocalDateTime now = LocalDateTime.now();
        int allowedDays = Arrays.asList("1008", "1009", "2008").contains(b.getWarehouseCode())
                ? 10
                : 5;
        LocalDateTime earliestAllowedDate = now.plusDays(allowedDays);

        // 将Date类型转换为LocalDateTime进行比较（使用系统默认时区）
        LocalDateTime planArrivalDateTime = b.getPlanArrivalDate().toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();

        // 校验预交货日期
        if (planArrivalDateTime.isAfter(earliestAllowedDate)) {
            String errorMsg = String.format(
                    "%s,%s,%s,%s,预交货日超出允许范围（当前日期+%d天）,请联系采购员:%s,修改交期/退回物料",
                    b.getPurchaseOrder(), b.getCustomerNo(), b.getItemCode(), b.getItemName(), allowedDays,b.getEmployeeName()
            );
            throw new RuntimeException(errorMsg);
        }
    }

    @Override
    public List<PurchaseReceiptStorageVo> getPurcharseByBarcode(PurcharseDeliveryDto dto) {
        //先判断条码是否存在
        int i = purchaseMapper.getPurcharseBarcodeIsNull(dto.getNumber());
        if( i == 0){
            throw new RuntimeException("条码不存在!");
        }


        barcodeLimitByPurchase(dto.getNumber());

        List<PurchaseReceiptStorageVo> list = new ArrayList<>();
        // 查询条码信息
        String barType = bmBarcodeDetailMapper.getBarType(dto.getNumber());
        if (barType.equals("4")) {
            List<PurchaseReceiptStorageVo> list1 =purchaseMapper.getPurcharseByBarcode(dto);
            if (list1 != null) {
                list.addAll(list1);
            }
        }
        //如果是容器码，要查出子条码返回
        if (barType.equals("5")) {
            List<PurchaseReceiptStorageVo> list2 = purchaseMapper.getPurcharseByChildBarcode(dto.getNumber());
            list2.forEach(vo1 -> {
                barcodeLimitByPurchase(vo1.getBarcode());
            });
            if (list2 != null) {
                list.addAll(list2);
            }
        }

        if(list.isEmpty()){
            throw new RuntimeException("条码已到货|检验|入库");
        }
        return list;
    }




    @Override
    public int inserinvBarcodeOperation(Result<List<PurchaseReceiptStorageVo>> purcharsedeliveryNo) {
        List<InvBarcodeOperation> invBarcodeOperationList = new ArrayList<>();

//        for (PurchaseReceiptStorageVo vo : purcharsedeliveryNo.getData()) {
//            InvBarcodeOperation invBarcodeOperation = InvBarcodeOperation.builder()
//                    .id(UUID.randomUUID().toString())
//                    .docOrgNo("abl")
//                    .orgNo("")
//                    .warehouseNo(vo.getWarehouseCode())
//                    .cellNo(vo.getBinCode())
//                    .itemNo(vo.getItemCode())
//                    .barcode(vo.getBarcode())
//                    .createBy(vo.getCreateBy())
//                    .createDate(DateUtil.date() )
//                    .updateBy(vo.getCreateBy())
//                    .createFlag(0)
//                    .lotAtt02(vo.getSupplierNo())
//                    .lotAtt03(vo.getSupplierName())
//                    .lotAtt04(vo.getItemName())
//                    .lotAtt05(vo.getItemSpec())
//                    .lotAtt06(vo.getMatchQty())
//                            .build();
//            invBarcodeOperationList.add(invBarcodeOperation);
//
//        }
//       int i =  purchaseMapper.inserinvBarcodeOperation(invBarcodeOperationList);
//        return i;
        int i= 1;
    return i;
    }

    @Override
    public Result<List<PurchaseReceiptStorageVo>> getBarCodeOperation() {
        List<PurchaseReceiptStorageVo> list = purchaseMapper.getBarCodeOperation();
        return Result.ok(list);
    }


    /**
     * 采购到货检验单头
     * @param dto
     * @return
     */
    @Override
    public Result<PageInfo<PurchaseArrivalCheckHeader>> ListPurchaseArrivalStorageCheck(PurchaseArrivalStorageCheckDto dto) {
        String barcode = dto.getBarcode();
        System.out.println("dto.getBarcode() = " + barcode);

        //根据箱码查品号和到货单号
        PurchaseArrivalCheckHeader p = purchaseMapper.getItemCodeAndArrivalDocNo(barcode);

        if (p.getArrivalDocNo() == null || p.getItemCode() == null){
            return Result.fail(new PageInfo<>(),"箱码不存在对应的品号和到货单号!");
        }
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<PurchaseArrivalCheckHeader> headerList = purchaseMapper.ListPurchaseArrivalCheckHeader(p);
        return Result.ok(new PageInfo<>(headerList));
    }






    @Override
    public Result<PageInfo<DefectiveReasons>> ListDefectiveReasons(DefectiveReasonsDto dto) {
        PageHelper.startPage(dto.getCurrent(),dto.getSize());

        List<DefectiveReasons> reasonList =   purchaseMapper.ListDefectiveReasons(dto.getReasons());
        PageInfo<DefectiveReasons> pageInfo = new PageInfo<>(reasonList);
        return Result.ok(pageInfo);
    }

    @Override
    public Result<List<PurchaseReceiptStorageVo>> getArrivalNo(PurcharseDeliveryDto dto) {
        String barcode = dto.getNumber();
//        String lotatt30 = purchaseMapper.selectBarcodelotatt30(barcode);
        //1、容器级码查出下级子码
        Integer  i = purchaseMapper.selectBarType(barcode);
        if(i != null && i == 5){
         String uuid =  purchaseMapper.selectFUuid(barcode);
        List<String> childList =  purchaseMapper.selectBarChild(uuid);
            List<PurchaseReceiptStorageVo> list= purchaseMapper.getArrivalNoByChildlist(childList);
            return Result.ok(list);
        }
        //2、直接箱码查询
        List<PurchaseReceiptStorageVo> list= purchaseMapper.getArrivalNo(dto);
        return Result.ok(list);
    }

    @Override
    public Result<PageInfo<PurchaseArrivalCheckHeader>> ListPurchaseArrivalStorageWaitCheck(PurchaseArrivalStorageWaitCheckDto dto) {

        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<PurchaseArrivalCheckHeader> headerList = purchaseMapper.ListPurchaseArrivalWaitCheckHeader();
        return Result.ok(new PageInfo<>(headerList));
    }

    @Override
    public Result<PageInfo<PurchaseReceiptStorageWaitCheckVo>> ListPurchaseReceiptStorageWaitCheck(PurchaseReceiptStorageWaitCheckDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<PurchaseReceiptStorageWaitCheckVo> list = purchaseMapper.ListPurchaseReceiptStorageWaitCheck(dto);
        return Result.ok(new PageInfo<>(list));

}

    @Override
    public Result<PageInfo<PurchaseReceiptStorageWaitCheckItemVo>> ListPurchaseReceiptStorageWaitCheckItem(PurchaseReceiptStorageWaitCheckItemDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<PurchaseReceiptStorageWaitCheckItemVo> list = purchaseMapper.ListPurchaseReceiptStorageWaitCheckItem(dto.getArrivalNo());
        return Result.ok(new PageInfo<>(list));
    }

    @Override
    public Result<PageInfo<Supplier>> ListSupplierCode(Supplier dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());

        List<Supplier> supplierList = purchaseMapper.ListSupplierCode(dto.getSupplierName());
        return Result.ok(new PageInfo<>(supplierList));
    }

    @Override
    public Result<PageInfo<PRSVo>> getArrivalInfo(PRSDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<PRSVo> resultList = purchaseMapper.getArrivalInfo(dto);
        PageInfo<PRSVo> pageInfo = new PageInfo<>(resultList);
        return Result.ok(pageInfo);
    }

    @Override
    public Result<PageInfo<PRSDVo>> getArrivalInfoD(PRSDDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<PRSDVo> resultList = purchaseMapper.getArrivalInfoD(dto);
        PageInfo<PRSDVo> pageInfo = new PageInfo<>(resultList);
        return Result.ok(pageInfo);
    }


    //
//    @Override
//    @Transactional
//    public Result createPurchaseArrival(PurchaseArrivalDto purchaseArrivalDto) {
//        //在eten中插入到货单
////        HttpHeaders requestHeaders = new HttpHeaders();
////        requestHeaders.set();
//        createPurchaseArrivalEten(purchaseArrivalDto);
//        return Result.ok().message("采购单生成成功");
//    }

    //    @Override
//    public Result createPurchaseReceipt(PurchaseArrivalDto purchaseArrivalDto) {
//        return null;
//    }
    //获取到货单数据
    //扫码入库简洁信息





    @Override
    public Result<List<PurchaseArrivalSimpleVo>>  getPurcharseArrivalInfo(PurchaseArrivalSimpleDto purchaseArrivalSimpleDto) {
        List<PurchaseArrivalSimpleVo> info = purchaseMapper.getPurcharseArrivalInfo(purchaseArrivalSimpleDto);
        if (info != null && !info.isEmpty()){
            return Result.ok(info).message("到货单简易信息获取成功");
        }else {
            return Result.fail(info).message("到货单简易信息获取失败");
        }

    }
    //扫码入库详细
    @Override
    public Result<List<PurchaseArrivalDetailVo>> getPurchaseArrivalDetail(PurchaseArrivalDetailDto purchaseArrivalDetailDto) {
        List<PurchaseArrivalDetailVo> list = new ArrayList<>();
        try {
            list = purchaseMapper.getPurchaseArrivalDetail(purchaseArrivalDetailDto);
            if (!list.isEmpty()){
                return Result.ok(list).message("查询成功");
            }else {
                return Result.ok(list).message("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(list).message("查询失败");
        }

    }
    //扫码入库汇总
    @Override
    public Result getPurcharseArrivalDetailByDocNo(PurchaseArrivalDetailDto purchaseArrivalDetailDto) {
        List<PurchaseArrivalDetailCollectVo> list= purchaseMapper.getPurcharseArrivalDetailByDocNo(purchaseArrivalDetailDto);
        return Result.ok(list).message("获取采购到货详细信息");
    }
    //扫码入库中间表
//    @Transactional
//    @Override
//    public MiddleReturnDto  insertMiddleTable(List<ScanToStorageDto> list) {
//        DateTime date = DateUtil.date();
//        List<String> docNo = new ArrayList<>();
//        for (ScanToStorageDto dto : list) {
//            String barcode = dto.getBarcode();
//            docNo.add(dto.getDocNo());
//            purchaseMapper.insertMiddleTable(new Lsmdt(
//                    UUID.randomUUID().toString().toUpperCase(),                               //主键
//                    barcode,                    //条码编号
//                    dto.getWarehouseCode(),              //仓库
//                    dto.getBinCode(),                    //库位
//                    barcode.substring(barcode.indexOf("#")+1),                    //批号
//                    dto.getMatchQty(),                   //异动数量
//                    1,                                   //异动类型
//                    dto.getDocNo(),                      //来源单号
////                "0",                                //来源项次
////                null,                               //目的单号
////                "0",                                //目的项次
//                    "admin",                            //PDA操作人代号
//                    DateUtil.format(date,"yyyy-MM-dd"),              //扫描日期
//                    DateUtil.format(date,"HH:mm:ss.SSS"),               //时间
//                    "Y",                                //扣账资料否
//                    dto.getUnitCode(),                   //异动数量单位
//                    1.0,                                //异动与库存单位换算率
//                    SCANTOSTORAGE_APPID,                               //终端设备编号
//                    SCANTOSTORAGE_APPID+"A005"+SCANTOSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),  //APP申请串号
//                    "A005",                             //APP申请所属模组
//                    "N",                                //ERP异动码
//                    "",                               //备注
//                    "1",                                //条码异动类型
//                    UUID.randomUUID().toString().trim().replace("-",""),//APP申请序列号
//                    "004",                              //039
//                    "NNY",                              //042
//                    dto.getUnitCode(),                   //046
//                    "0",                                //047
//                    dto.getScrapQty(),
//                    dto.getRejectQty(),
//                    "9998-12-31",                       //058
//                    dto.getItemCode(),                   //物料代码
//                    UUID.randomUUID().toString().trim().replace("-",""),//字符保留栏位
//                    DateUtil.format(DateUtil.date(),"yyyy-MM-dd"),                 //过账日期
//                    DateUtil.format(DateUtil.date(),"yyyy-MM-dd"),                 //日期保留栏位
//                    DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"), //资料创建日
//                    "1",                                //资料建立者
//                    WMS_ENTID,                          //企业代码
//                    DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"),//最近修改日
//                    "1",                                //资料修改者
//                    "qqq",                              //资料所属部门
//                    "dcms",                             //资料所有者
//                    WMS_COMPANYID,                      //营运据点
//                    "Y"                                //状态码
//            ));
//        }
//
//
//
////        return new MiddleReturnDto(docNo.get(0),SCANTOSTORAGE_APPID,SCANTOSTORAGE_APPID+"A005"+SCANTOSTORAGE_APPVERSION+DateUtil.format(date,"yyMMddhhmmss"),"");
//        return new MiddleReturnDto(docNo.get(0),SCANTOSTORAGE_APPID,"unknown02:00:00:00:00:00ab1cbf8632fb41ceA0054.223240307153009","");
//
//    }
    //扫码入库提交
    @Override
    public JSONObject scanToStorageSubmit(MiddleReturnDto middleReturnDto) {
        EtenSession etenSession = new EtenSession(SCANTOSTORAGE_KEY,WMS_TYPE,
                new SessionHost(WMS_HOST_PROD,WMS_HOST_IP,WMS_HOST_LANG,WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getErpRemark()),
                new SessionService(WMS_SERVICE_PROD,SCANTOSTORAGE_NAME,WMS_SERVICE_IP,WMS_SERVICE_ID),
                new SessionDatakey(WMS_ENTID,WMS_COMPANYID),
                new SessionPayload(new StdData(new ScanToStorageParameter(
                        "","","","","","","","","","","","","","",
                        new ScanToStorageParameterData(
                                "", middleReturnDto.getDocNo(), middleReturnDto.getDocNo(), "","","","","","","","","","","","",""
                        ),
                        new ScanToStorageParameterHost(
                                WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getAppid(),SCANTOSTORAGE_APPMODULE,SCANTOSTORAGE_APPVERSION,
                                SCANTOSTORAGE_NAME,WMS_ENTID,WMS_COMPANYID,middleReturnDto.getErpRemark(),"0",""
                        )
                ))));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type","text/plain; charset=utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(SERVICE_IP_PORT+"/Restful/invokeSrv", httpEntity, String.class);
        JSONObject result = JSON.parseObject(response);
        return result;
    }

    /**
     * 采购入库提交
     * @param middleReturnDto
     * @return
     */
    @Override
    public JSONObject PurchaseToStorageSubmit(MiddleReturnDto middleReturnDto) {
        System.out.println("提交");
        EtenSession etenSession = new EtenSession(MO_INSTORAGE_KEY, WMS_TYPE,
                new SessionHost(WMS_HOST_PROD, WMS_HOST_IP, WMS_HOST_LANG, WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getErpRemark()),
                new SessionService(WMS_SERVICE_PROD, PURCHASE_INSTORAGE_NAME, WMS_SERVICE_IP, WMS_SERVICE_ID),
                new SessionDatakey(WMS_ENTID, WMS_COMPANYID),
                new SessionPayload(new StdData(new ScanToStorageParameter(
                        "", "", "", "", "", "", "", "", "", "", "", "", "", "",
                        new ScanToStorageParameterData(
                                "", middleReturnDto.getDocNo(), middleReturnDto.getDocNo(), "", "", "", "", "", "", "", "", "", "", "", "", ""
                        ),
                        new ScanToStorageParameterHost(
                                WMS_HOST_ACCT, middleReturnDto.getTimestamp(), middleReturnDto.getAppid(),
                                PURCHASE_INSTORAGE_APPMODULE, PURCHASE_INSTORAGE_APPVERSION, PURCHASE_INSTORAGE_NAME, WMS_ENTID, WMS_COMPANYID,
                                middleReturnDto.getErpRemark(), "0", ""
                        )
                ))));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "text/plain; charset=utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(etenSession), httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(SERVICE_IP_PORT + "/Restful/invokeSrv", httpEntity, String.class);
        System.out.println(response);
        System.out.println(httpEntity.getBody());
        JSONObject result = JSON.parseObject(response);
        return result;
    }


    //在eten中生成到货单
    public void createPurchaseArrivalEten(PurchaseArrivalDto purchaseArrivalDto) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("docNo",purchaseArrivalDto.getDocNo());
        List<PurchaseArrival> purchaseArrivals = purchaseArrivalMapper.selectByMap(map);
        //判断是否已经有数据
        //没有则插入
        if (purchaseArrivals==null||purchaseArrivals.isEmpty()){
            //设置插入的数据
            PurchaseArrival purchaseArrival = setPurchaseArrival(purchaseArrivalDto);
            purchaseArrivalMapper.insert(purchaseArrival);
        }

    }
    //为到货单添加值
    private PurchaseArrival setPurchaseArrival(PurchaseArrivalDto purchaseArrivalDto) {
        PurchaseArrival p = new PurchaseArrival();
        String str = purchaseArrivalDto.getDocNo().substring(purchaseArrivalDto.getDocNo().indexOf("-"));
        p.setDocNo(purchaseArrivalDto.getDocCode()+str.substring(0,7));
        Doc doc = docMapper.selectBydocCode(purchaseArrivalDto.getDocCode());
        p.setDocDate(purchaseArrivalDto.getBusinessTime());
//        p.setDocId(doc.getDocId());
//        p.setCategory(doc.getCategory());
        p.setPurchaseArrivalId(UUID.randomUUID().toString());
        p.setArrivalDate(purchaseArrivalDto.getBusinessTime());
        HashMap<String, Object> map = new HashMap<>();
        purchaseOrderMapper.selectByMap((Map<String, Object>) new HashMap<String,Object>().put("DOC_NO",purchaseArrivalDto.getDocNo()));
        return p;
    }
    //从redis中获取purchase数据
//    public List<PurchaseSumDto> getFromRedis(String appModule){
//        List<PurchaseSumDto> purchaseList = new ArrayList<>();
//        //从redis中找到需要返回的值
//        List<String> purchaseLists = stringRedisTemplate.opsForList().range(PURCHASE_CODE_KEY+":"+appModule, 0, -1);
//        //找打就返回
//        // 2.判断 Redis 中是否有该缓存
//        if (purchaseLists != null && !purchaseLists.isEmpty()) {
//            // 2.1.若 Redis 中存在该缓存，则直接返回
//            for (String str : purchaseLists) {
//                purchaseList.add(JSONUtil.toBean(str, PurchaseSumDto.class));
//            }
//            return purchaseList;
//        }else {
//            return null;
//        }
//    }
    //在redis中添加缓存
//    public void setPurchaseCache(List<PurchaseSumDto> purchaseList,String appModule){
//        //采购收货
//        if (appModule.equals("A001")){
//            //在redis中添加缓存
//            for (PurchaseSumDto purchaseSumDto : purchaseList) {
//                stringRedisTemplate.opsForList().rightPushAll(PURCHASE_CODE_KEY+":"+appModule, JSONUtil.toJsonStr(purchaseSumDto));
//            }
//        }
//        //扫码入库
//        if (appModule.equals("A005")){
//            //在redis中添加缓存
//            for (PurchaseSumDto purchaseSumDto : purchaseList) {
//                stringRedisTemplate.opsForList().rightPushAll(PURCHASE_CODE_KEY+":"+appModule, JSONUtil.toJsonStr(purchaseSumDto));
//            }
//        }
//        //采购仓退
//        if (appModule.equals("A008")){
//            //在redis中添加缓存
//            for (PurchaseSumDto purchaseSumDto : purchaseList) {
//                stringRedisTemplate.opsForList().rightPushAll(PURCHASE_CODE_KEY+":"+appModule, JSONUtil.toJsonStr(purchaseSumDto));
//            }
//        }
//    }

}
